<?php

namespace common\models\business;

use common\components\Helper;
use common\models\wechat\WechatFans;
use Yii;

/**
 * This is the model class for table "business_person".
 *
 * @property string $id
 * @property string $name
 * @property string $gender
 * @property string $phone
 * @property string $descripation
 * @property string $avatar
 * @property string $openid
 * @property integer $status
 * @property integer $createtime
 */
class BusinessPerson extends \yii\db\ActiveRecord
{

    //主要用于日志记录
    public $uselog = [
        'tabcomment' => '业务员',      //用于修改了什么的显示
        'mainfield' => 'name',      //主要的显示标题的字段
        'picturefield' => ['identity_img'],        //图片字段
        'timefield' => [],       //时间字段
        'unsetfiled' =>['avatar'],      //不需要记录的字段
    ];



    const STATUS_QUIT = 0;
    const STATUS_JOB = 1;
    public static $status = [
        self::STATUS_JOB => '在职',
        self::STATUS_QUIT => '离职'
    ];



    public static $gender = [
        '男'=>'男','女'=>'女'
    ];



    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'business_person';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['name', 'gender', 'phone', 'descripation', 'openid', 'status', 'createtime','identityno'], 'required'],
            [['status', 'createtime'], 'integer'],
            [['name','identityno'], 'string', 'max' => 20],
            [['gender'], 'string', 'max' => 2],
            [['phone'], 'string', 'max' => 15],
            [['descripation'], 'string', 'max' => 500],
            [['openid'], 'string', 'max' => 60],
            [['identity_img'], 'image', 'extensions' => 'jpg, jpeg, gif, png', 'on' => ['create', 'update']],
        ];
    }


    public function scenarios()
    {
        $scenarios = parent::scenarios();//本行必填，不写的话就会报如上错误
        $scenarios['create'] = ['identity_img'];
        $scenarios['update'] = ['identity_img'];

        return $scenarios;
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'name' => '姓名',
            'gender' => '性别',
            'phone' => '手机',
            'descripation' => '简介',
            'avatar' => '头像',
            'openid' => '对应的粉丝',
            'identityno' => '身份证号',
            'status' => '状态',
            'createtime' => '创建时间',
            'identity_img' => '身份证件照',
            'pigs' => '巡检的猪仔'
        ];
    }


    public function getFans()
    {
        return $this->hasOne(WechatFans::className(),['openid'=>'openid']);
    }


    public function getPigs()
    {
        return $this->hasMany(BusinessPig::className(),['headpersonid'=>'id']);
    }

}
